<?php
namespace Admin\Controller;

use Think\Controller;

class UsersController extends HomeController
{

    public function _initialize()
    {
        $storeList = D('Stores')->getStoresAllList();
        $this->assign('storeList', $storeList);
    }

    /*
     * Function:        usersList
     * Description:    会员列表
     */
    public function usersList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter = returnFilter($filter);
        $usersList = D('User')->getUserList($page, $filter, $pageSize);
        if (is_array($usersList) && count($usersList) > 0) {
            foreach ($usersList as $key => $val) {
                $usersList[$key]['username'] = emojiDecode($val['username']);
            }
        }
        $count = D('User')->getUserCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('usersList', $usersList);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->display();
    }

    /*
     * Function:        upgradeUsers
     * Description:    会员升级
     * @param:        $id int 会员id
     */
    public function upgradeUsers($id = '')
    {
        if (!$usersInfo = D('Users')->getUsersInfo($id)) {
            $ret = array('code' => 0, 'message' => '不存在此用户');
            $this->ajaxReturn($ret);
        }
        $data = array();
        $data['id'] = $id;
        $data['level'] = 1;
        if (D('Users')->updateUsers($data)) {
            $ret = array('code' => 1, 'message' => '升级成功');
            $this->ajaxReturn($ret);
        } else {
            $ret = array('code' => 0, 'message' => '升级失败!');
            $this->ajaxReturn($ret);
        }
    }

    /*
     * Function:        lookUsers
     * Description:    会员查看
     * @param:        $id int 会员id
     */
    public function lookUsers($id = '')
    {
        if (empty($id)) {
            $this->error('参数异常');
        } else {
            $usersInfo = D('User')->getUserInfo($id);
            $this->assign('usersInfo', $usersInfo);
            $this->display();
        }
    }

    /*
     * Function:        createUsersNumber
     * Description:    生成用户编号
     */
    public function createUsersNumber()
    {
        $n = 0;
        $userList = D('User')->getUserAll();
        if (is_array($userList) && count($userList) > 0) {
            foreach ($userList as $key => $val) {
                $data = array();
                $data['id'] = $val['id'];
                $data['number_no'] = D('User')->generateNumberNo();
                if (D('User')->updateUser($data)) {
                    $n++;
                }
            }
        }
        echo $n;
    }

    /*
     * Function:        userGalleryList
     * Description:    会员画廊列表
     */
    public function userGalleryList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter['flag'] = 1;
        $filter = returnFilter($filter);
        $list = D('Gallerys')->getGallerysList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => $val) {
                if (!empty($val['user_id'])) {
                    $userInfo = D('User')->getUserInfo($val['user_id']);
                    $list[$key]['avatarurl'] = $userInfo['avatarurl'];
                }
            }
        }
        $count = D('Gallerys')->getGallerysCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $statusList = [['id' => 0, 'name' => '待审核'], ['id' => 1, 'name' => '已审核'], ['id' => 2, 'name' => '审核失败']];
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->assign('statusList', $statusList);
        $this->display();
    }

    public function auitUserGallery($id = '')
    {
        if (IS_POST) {
            $arr = array();
            $data = I('post.');
            if (!$info = M('gallerys')->where(['id' => $data['id']])->find()) {
                $ret = array('code' => 0, 'message' => '不存在此用户画廊记录!');
                $this->ajaxReturn($ret);
            }
            if ($data['auitSelect'] == 1) {
                $arr['status'] = 1;
                $arr['op_memo'] = '';
            } else if ($data['auitSelect'] == 2) {
                $arr['status'] = 2;
                if (empty($data['reason'])) {
                    $ret = array('code' => 0, 'message' => '审核不通过原因不能为空!');
                    $this->ajaxReturn($ret);
                }
                $arr['reason'] = $data['reason'];
            }
            $ret = M('gallerys')->where(['id' => $data['id']])->save($arr);
            if ($ret !== false) {
                $ret = array('code' => 1, 'message' => '提交成功!');
                $this->ajaxReturn($ret);
            } else {
                $ret = array('code' => 0, 'message' => '提交失败!');
                $this->ajaxReturn($ret);
            }
        } else {
            $this->assign('id', $id);
            $info = M('gallerys')->where(['id' => $id])->find();
            $imgList = explode(',', $info['images_url']);
            $this->assign('info', $info);
            $this->assign('imgList', $imgList);
            $auitSelectList = array(array('value' => 1, 'name' => '通过'), array('value' => 2, 'name' => '不通过'));
            $this->assign('auitSelectList', $auitSelectList);
            $this->display();
        }
    }

    /*
     * Function:        userGalleryDetail
     * Description:    用户画廊详情
     * @param:        $id
     */
    public function userGalleryDetail($id = '')
    {
        $info = M('gallerys')->where(['id' => $id])->find();
        $imgList = explode(',', $info['images_url']);
        $this->assign('info', $info);
        $this->assign('imgList', $imgList);
        $this->display();
    }

    /*
     * Function:        deleteUserGallery
     * Description:    删除用户画廊
     * @param:        $id
     */
    public function deleteUserGallery($id = '')
    {
        $ret = M('gallerys')->where(['id' => $id])->save(['is_deleted' => 1]);
        if ($ret !== false) {
            $ret = array('code' => 1, 'message' => '操作成功');
            $this->ajaxReturn($ret);
        } else {
            $ret = array('code' => 0, 'message' => '操作失败!');
            $this->ajaxReturn($ret);
        }
    }

    /*
     * Function:        topUserGallery
     * Description:    置顶用户画廊
     * @param:        $id
     */
    public function topUserGallery($id = '')
    {
        //先解除置顶
        M('gallerys')->where(['is_top' => 1])->save(['is_top' => 0]);
        if (M('gallerys')->where(['id' => $id])->save(['is_top' => 1])) {
            $ret = array('code' => 1, 'message' => '操作成功');
            $this->ajaxReturn($ret);
        } else {
            $ret = array('code' => 0, 'message' => '操作失败!');
            $this->ajaxReturn($ret);
        }
    }

    /*
     * Function:        userIntegralLogsList
     * Description:    会员积分日志列表
     */
    public function userIntegralLogsList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter['flag'] = 1;
        $filter = returnFilter($filter);
        $list = D('UserIntegralLogs')->getUserIntegralLogsList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => $val) {
                if (!empty($val['user_id'])) {
                    $userInfo = D('User')->getUserInfo($val['user_id']);
                    $list[$key]['username'] = $userInfo['username'];
                }
            }
        }
        $count = D('UserIntegralLogs')->getUserIntegralLogsCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->display();
    }

    /*
     * Function:        userCouponList
     * Description:    会员优惠券列表
     */
    public function userCouponList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter['flag'] = 1;
        $filter = returnFilter($filter);
        $list = D('UserCoupon')->getUserCouponList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => $val) {
                if (!empty($val['user_id'])) {
                    $userInfo = D('User')->getUserInfo($val['user_id']);
                    $list[$key]['username'] = $userInfo['username'];
                }
            }
        }
        $count = D('UserCoupon')->getUserCouponCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->display();
    }

    /*
     * Function:        userGalleryCommentList
     * Description:    会员画廊评论列表
     */
    public function userGalleryCommentList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter['flag'] = 1;
        $filter = returnFilter($filter);
        $list = D('UserGalleryComment')->getUserGalleryCommentList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => $val) {
                if (!empty($val['user_id'])) {
                    $userInfo = D('User')->getUserInfo($val['user_id']);
                    $list[$key]['username'] = $userInfo['username'];
                }
                $galleryInfo = D('Gallerys')->getGallerysInfo($val['tid']);
                $list[$key]['title'] = $galleryInfo['title'];
            }
        }
        $count = D('UserGalleryComment')->getUserGalleryCommentCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $statusList = [['id' => 0, 'name' => '待审核'], ['id' => 1, 'name' => '已审核'], ['id' => 2, 'name' => '审核失败']];
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->assign('statusList', $statusList);
        $this->display();
    }

    /*
     * Function:        deleteUserGalleryComment
     * Description:    删除用户画廊
     * @param:        $id
     */
    public function deleteUserGalleryComment($id = '')
    {
        $ret = M('user_gallery_comment')->where(['id' => $id])->save(['is_deleted' => 1]);
        if ($ret !== false) {
            $ret = array('code' => 1, 'message' => '操作成功');
            $this->ajaxReturn($ret);
        } else {
            $ret = array('code' => 0, 'message' => '操作失败!');
            $this->ajaxReturn($ret);
        }
    }

    public function auitUserGalleryComment($id = '')
    {
        if (IS_POST) {
            $arr = array();
            $data = I('post.');
            if (!$info = M('user_gallery_comment')->where(['id' => $data['id']])->find()) {
                $ret = array('code' => 0, 'message' => '不存在此用户评论记录!');
                $this->ajaxReturn($ret);
            }
            if ($data['auitSelect'] == 1) {
                $arr['status'] = 1;
                $arr['op_memo'] = '';
            } else if ($data['auitSelect'] == 2) {
                $arr['status'] = 2;
                if (empty($data['reason'])) {
                    $ret = array('code' => 0, 'message' => '审核不通过原因不能为空!');
                    $this->ajaxReturn($ret);
                }
                $arr['reason'] = $data['reason'];
            }
            $ret = M('user_gallery_comment')->where(['id' => $data['id']])->save($arr);
            if ($ret !== false) {
                $ret = array('code' => 1, 'message' => '提交成功!');
                $this->ajaxReturn($ret);
            } else {
                $ret = array('code' => 0, 'message' => '提交失败!');
                $this->ajaxReturn($ret);
            }
        } else {
            $this->assign('id', $id);
            $cInfo = M('user_gallery_comment')->where(['id' => $id])->find();
            $userInfo = D('User')->getUserInfo($cInfo['user_id']);
            $cInfo['username'] = $userInfo['username'];
            $info = M('gallerys')->where(['id' => $cInfo['tid']])->find();
            $imgList = explode(',', $info['images_url']);
            $this->assign('info', $info);
            $this->assign('cInfo', $cInfo);
            $this->assign('imgList', $imgList);
            $auitSelectList = array(array('value' => 1, 'name' => '通过'), array('value' => 2, 'name' => '不通过'));
            $this->assign('auitSelectList', $auitSelectList);
            $this->display();
        }
    }

    /*
     * Function:        deleteGalleryComment
     * Description:    删除用户画廊评论
     * @param:        $id
     */
    public function deleteGalleryComment($id = '')
    {
        if (M('user_gallery_comment')->where(['id' => $id])->save(['status' => 1])) {
            $ret = array('code' => 1, 'message' => '操作成功');
            $this->ajaxReturn($ret);
        } else {
            $ret = array('code' => 0, 'message' => '操作失败!');
            $this->ajaxReturn($ret);
        }
    }

    /*
     * Function:        updateUserIntegral
     * Description:    会员积分增加
     */
    public function updateUserIntegral($id = '')
    {
        $ret = array();
        if (IS_POST) {
            $data = I('post.');
            if (!$userInfo = D('User')->getUserInfo($data['id'])) {
                $ret = array('code' => 0, 'message' => '不存在此用户!');
                $this->ajaxReturn($ret);
            }
            $result = false;
            if ($data['type'] == 1) { //加积分
                //增加积分记录
                $integral = [];
                $integral['type'] = 1;
                $integral['user_id'] = $data['id'];
                $integral['title'] = '后台增加积分';
                $integral['integral'] = $data['integral'];
                $integral['addtime'] = date('Y-m-d H:i:s', time());
                M('user_integral_logs')->add($integral);
                //增加用户积分
                $uData = array();
                $uData['integral'] = array('exp', 'integral+' . $integral['integral']);
                $result = M('user')->where(['id' => $integral['user_id']])->save($uData);
            } else if ($data['type'] == 2) { //减积分
                if (($userInfo['integral'] - abs($data['integral'])) < 0) {
                    $data['integral'] = $userInfo['integral'];
                }
                //减少积分记录
                $integral = [];
                $integral['type'] = 2;
                $integral['user_id'] = $data['id'];
                $integral['title'] = '后台减少积分';
                $integral['integral'] = abs($data['integral']);
                $integral['addtime'] = date('Y-m-d H:i:s', time());
                M('user_integral_logs')->add($integral);
                //增加用户积分
                $uData = array();
                $uData['integral'] = array('exp', 'integral-' . $integral['integral']);
                $result = M('user')->where(['id' => $integral['user_id']])->save($uData);
            }
            if ($result !== false) {
                $ret = array('code' => 1, 'message' => '操作成功!');
                $this->ajaxReturn($ret);
            } else {
                $ret = array('code' => 0, 'message' => '操作失败!');
                $this->ajaxReturn($ret);
            }
        } else {
            $usersInfo = D('User')->getUserInfo($id);
            $this->assign('usersInfo', $usersInfo);
            $this->display();
        }
    }

    /*
     * Function:        userRechargeList
     * Description:    会员充值列表
     */
    public function userRechargeList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter['flag'] = 1;
        $filter['status'] = 1;
        $filter = returnFilter($filter);
        $list = D('UserRechargeLogs')->getUserRechargeLogsList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                if (!empty($val['user_id'])) {
                    $userInfo = D('User')->getUserInfo($val['user_id']);
                    $list[$key]['username'] = $userInfo['username'];
                }
                $val['type_name'] = '无';
                if ($val['type'] == 1) {
                    $val['type_name'] = '增加';
                } else {
                    $val['type_name'] = '减少';
                }
                $val['status_name'] = '无';
                if ($val['status'] == 1) {
                    $val['status_name'] = '充值成功';
                } else {
                    $val['status_name'] = '未充值';
                }
            }
        }
        $count = D('UserRechargeLogs')->getUserRechargeLogsCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->display();
    }

    /*
     * Function:        userCardList
     * Description:    会员卡列表
     */
    public function userCardList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter = returnFilter($filter);
        $list = D('UserCard')->getUserCardList($page, $filter, $pageSize);
        if (is_array($list) && count($list) > 0) {
            foreach ($list as $key => &$val) {
                $val['store_name'] = M('stores_list')->where(['id' => $val['store_id']])->getField('title');
                $user_name = M('user')->where(['id' => $val['user_id']])->getField('username');
                $val['user_name'] = emojiDecode($user_name);
                $val['goods_title'] = M('goods')->where(['id' => $val['goods_id']])->getField('goods_title');
                $sy_nums = ($val['nums'] - $val['freez']) < 0 ? 0 : ($val['nums'] - $val['freez']);
                $val['sy_nums'] = $sy_nums;
                $val['nums'] = M('goods')->where(['id' => $val['goods_id']])->getField('total_nums');
                if (empty($userInfo['avatarurl'])) {
                    $val['avatarurl'] = 'https://hezhen.nbqinling.com/Uploads/user/2023-11-23/655eef6b39c8d.jpg';
                }
            }
        }
        $count = D('UserCard')->getUserCardCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->display();
    }

    /*
     * Function:        vipList
     * Description:    VIP会员列表
     */
    public function vipList()
    {
        $filter = I('param.');
        $page = I('get.p', 1);
        $pageSize = 10;
        $filter['level'] = 2;
        $filter = returnFilter($filter);
        $usersList = D('User')->getUserList($page, $filter, $pageSize);
        if (is_array($usersList) && count($usersList) > 0) {
            foreach ($usersList as $key => $val) {
                $usersList[$key]['store_name'] = M('stores_list')->where(['id' => $val['store_id']])->getField('title');
                $usersList[$key]['username'] = emojiDecode($val['username']);
                $usersList[$key]['source_name'] = $this->switchSource($val['source']);
                if (empty($val['avatarurl'])) {
                    $usersList[$key]['avatarurl'] = 'https://hezhen.nbqinling.com/Uploads/user/2023-11-23/655eef6b39c8d.jpg';
                }
            }
        }
        $count = D('User')->getUserCount($filter);
        $pages = getpage($count, $pageSize, $filter);
        $show = $pages->show();
        $this->assign('usersList', $usersList);
        $this->assign('page', $show);
        $this->assign('filter', $filter);
        $this->display();
    }

    /*
     * Function:        addVip
     * Description:    会员添加
     */
    public function addVip()
    {
        $ret = array();
        if (IS_POST) {
            $data = I('post.');
            $data['nickname'] = $data['username'];
            $data['level'] = 2;
            if (D('User')->addUser($data)) {
                $ret = array('code' => 1, 'message' => '添加成功');
                $this->ajaxReturn($ret);
            } else {
                $ret = array('code' => 0, 'message' => '添加失败!');
                $this->ajaxReturn($ret);
            }
        } else {
            $sourceList = [['name' => '微信', 'value' => 'weixin'], ['name' => '美团', 'value' => 'meituan'], ['name' => '大众点评', 'value' => 'dazhong'], ['name' => '抖音', 'value' => 'douyin']];
            $this->assign('sourceList', $sourceList);
            $this->display();
        }
    }

    /*
     * Function:        editVip
     * Description:    会员编辑
     * @param:        $id int 会员id
     */
    public function editVip($id = '')
    {
        if (IS_POST) {
            $data = I('post.');
            $data['nickname'] = $data['username'];
            if (D('User')->updateUser($data)) {
                $ret = array('code' => 1, 'message' => '更新成功');
                $this->ajaxReturn($ret);
            } else {
                $ret = array('code' => 0, 'message' => '更新失败!');
                $this->ajaxReturn($ret);
            }
        } else {
            if (empty($id)) {
                $this->error('参数异常');
            } else {
                $info = D('User')->getUserInfo($id);
                $this->assign('info', $info);
                $sourceList = [['name' => '微信', 'value' => 'weixin'], ['name' => '美团', 'value' => 'meituan'], ['name' => '大众点评', 'value' => 'dazhong'], ['name' => '抖音', 'value' => 'douyin']];
                $this->assign('sourceList', $sourceList);
                $this->display();
            }
        }
    }

    /*
     * Function:        addUserCard
     * Description:    增加会员卡
     * @param:        $id int 会员id
     */
    public function addUserCard($id = '')
    {
        if (IS_POST) {
            $data = I('post.');
            $valid_time = $data['valid_time'];
            unset($data['valid_time']);
            $data['begin_time'] = date('Y-m-d', time());
            $data['end_time'] = date('Y-m-d', strtotime("+" . $valid_time . " day"));
            if (D('UserCard')->addUserCard($data)) {
                $ret = array('code' => 1, 'message' => '添加成功');
                $this->ajaxReturn($ret);
            } else {
                $ret = array('code' => 0, 'message' => '添加失败!');
                $this->ajaxReturn($ret);
            }
        } else {
            if (empty($id)) {
                $this->error('参数异常');
            } else {
                $userInfo = D('User')->getUserInfo($id);
                $this->assign('userInfo', $userInfo);
                $goodsList = M('goods')->where(['type' => 2, 'status' => 1, 'is_deleted' => 0])->select();
                $this->assign('goodsList', $goodsList);
                $this->display();
            }
        }
    }

    /*
     * Function:        editUserCard
     * Description:    编辑会员卡
     * @param:        $id int 会员id
     */
    public function editUserCard($id = '')
    {
        if (IS_POST) {
            $data = I('post.');
            if (D('UserCard')->editUserCard($data)) {
                $ret = array('code' => 1, 'message' => '更新成功');
                $this->ajaxReturn($ret);
            } else {
                $ret = array('code' => 0, 'message' => '更新失败!');
                $this->ajaxReturn($ret);
            }
        } else {
            if (empty($id)) {
                $this->error('参数异常');
            } else {
                $userInfo = D('User')->getUserInfo($id);
                $this->assign('userInfo', $userInfo);
                $goodsList = M('goods')->where(['type' => 2, 'status' => 1, 'is_deleted' => 0])->select();
                $this->assign('goodsList', $goodsList);
                $this->display();
            }
        }
    }

    public function switchSource($source = '')
    {
        $source_name = '';
        switch ($source) {
            case 'meituan':
                $source_name = '美团';
                break;
            case 'dazhong':
                $source_name = '大众点评';
                break;
            case 'douyin':
                $source_name = '抖音';
                break;
            case 'weixin':
                $source_name = '微信';
                break;
            default:
                $source_name = '未知';
                break;
        }
        return $source_name;
    }
}
